#!/usr/bin/env python3
# -*- coding:UTF-8 -*-
#日志模块
import logging

#日志级别:debug,info,warning,error,critical
#日志格式：
#日志输出:标注输出，日志文件

#基本功能1：终端输出日志
#logging.debug("user is %s logging" % 'liwanliang')
logging.log(logging.DEBUG,"user is %s logging."% 'liwanliang')
#logging.info("user %s attempted wrong password" % 'liwl')
logging.log(logging.INFO,"user is %s logging." % 'liwanliang')
#logging.warning("user %s attempted wrong password more than 3 times." % 'liwl')
#logging.error("select db timeout")
#logging.critical("server is down")

#基本功能2：格式化输出
#logging.basicConfig(level=logging.DEBUG, \
#                    format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s', \
#                    #datefmt='%a %d %b %Y %H:%M:%S')
#                    datefmt='%Y %H:%M:%S', \
#                    filename="py.log", \
#                    filemode='a')
#logging.debug("user is %s logging" % 'liwanliang')
#logging.info("user %s attempted wrong password" % 'liwl')
#logging.warning("user %s attempted wrong password more than 3 times." % 'liwl')
#logging.error("select db timeout")
#logging.critical("server is down")

#基本功能3：综合型输出logger
#logger是日志注册器，应用程序直接调用logger即可记录日志
#Handler日志句柄 ，发送日志到合适的地方
#Filter 提供过滤日志的方法
#Formatters 指定日志显示格式
#mylogger = logging.getLogger()
#mylogger.setLevel(logging.DEBUG)
#hterm = logging.StreamHandler()
#hterm.setLevel(logging.ERROR)
#hfile = logging.FileHandler("py3.log")
#hfile.setLevel(logging.INFO)
#myformat = logging.Formatter('%(asctime)s -%(name)s-%(levelname)s-%(message)s') #全局日志格式
#hterm.setFormatter(myformat)
#hfile.setFormatter(myformat)
#mylogger.addHandler(hterm)
#mylogger.addHandler(hfile)
#logging.debug("user is %s logging" % 'liwanliang')
#logging.info("user %s attempted wrong password" % 'liwl')
#logging.warning("user %s attempted wrong password more than 3 times." % 'liwl')
#logging.error("select db timeout")
#logging.critical("server is down")

#基本功能4：logger树型结构
##定义logger
logger = logging.getLogger() #root logger
logger01 = logging.getLogger("mylogger01")
logger02 = logging.getLogger("mylogger01") #logger01和logger02是同一个对象
logger03 = logging.getLogger("mylogger01.child") #logger03是logger01/02的子实例
##设置logger的日志级别
logger01.setLevel(logging.DEBUG)
logger02.setLevel(logging.INFO)
logger03.setLevel(logging.ERROR)
##设置日志输出
hterm = logging.StreamHandler() #输出到终端
hfile = logging.FileHandler("mypy3.log")
##设置日志格式
myformat = logging.Formatter('%(asctime)s -%(name)s-%(levelname)s-%(message)s') #全局日志格式
##载入日志格式
hterm.setFormatter(myformat)
hfile.setFormatter(myformat)
##载入日志记录仪
logger.addHandler(hterm)
logger.addHandler(hfile)
logger01.addHandler(hterm)
logger01.addHandler(hfile)
logger02.addHandler(hterm)
logger02.addHandler(hfile)
logger03.addHandler(hterm)
logger03.addHandler(hfile)
##调用
#logger.debug("logger.debug")
#logger.info("logger.info")
#logger.warning("logger.warning")
#logger.error("logger.error")
#logger.critical("logger.critiacl")
#logger01.debug("logger.debug")
#logger01.info("logger.info")
#logger01.warning("logger.warning")
#logger01.error("logger.error")
#logger01.critical("logger.critiacl")
#logger02.debug("logger.debug")
#logger02.info("logger.info")
#logger02.warning("logger.warning")
#logger02.error("logger.error")
#logger03.critical("logger.critiacl")
#logger03.debug("logger.debug")
#logger03.info("logger.info")
#logger03.warning("logger.warning")
#logger03.error("logger.error")
#logger03.critical("logger.critiacl")